﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_admin_save_property_let_info : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string hash = WebhelperMethods.URLHash(Request);
            if (string.IsNullOrWhiteSpace(hash))
            {
                Response.Redirect("login.aspx");
                return;
            }

            PomplingCustomers customer = PomplingCustomers.CustomerSelect(hash);
            if (string.IsNullOrWhiteSpace(customer.PomplingCustomerID))
            {
                Response.Redirect("login.aspx");
                return;
            }
            
            string sAddressId = WebhelperMethods.GetUrlParamValue(Globals.URL_VAR_ADDRESS_ID, Request);
            if (string.IsNullOrWhiteSpace(sAddressId))
            {
                Response.Redirect("admin_save_address.aspx?c=" + Request["c"] + "&a=");
                return;
            }
            //string urlParam = WebhelperMethods.GetUrlParams(Request);
            List<Lookup> lookupList = Lookup.GetLookupGroup("PropertyType");

            CmbPropertyType.DataTextField = "LookupText";
            CmbPropertyType.DataValueField = "LookupID";
            CmbPropertyType.DataSource = lookupList;
            CmbPropertyType.DataBind();
            
            int addressID = 0;
            Int32.TryParse(sAddressId, out addressID);
            PomplingProperty property = PomplingProperty.SelectAddress(addressID);

            // have we ever saved this page before? 
            if (property.NumberOfBedrooms < 1)
                return;
            CmbPropertyType.SelectedItem.Text = property.PropertyType;
            TxtNumberOfBedrooms.Text = property.NumberOfBedrooms.ToString();
            TxtPricePerCalendarMonth.Text = property.PricePerCalendarMonth.ToString();
           
            ChkIsFurnished.Checked = property.Furnished;
            TxtShortDescription.Text = property.ShortDescription;
            TxtLongDescription.Text = property.LongDescription;
        }
    }

    protected void SavePropertyLetInfo_Click(object sender, EventArgs e)
    {
        if (!Valid())
            return;
        PomplingProperty property = SavePropertyLetInfo();
        string newUri = "admin_save_property_attributes.aspx" + Globals.BuildAdminURLParams(Request, property.PomplingAddressID.ToString());       
        Response.Redirect(newUri);
    }

    private bool Valid()
    {
        LblError.Visible = false;
        LblError.Text = string.Empty;
        if (TxtNumberOfBedrooms.Text.Length == 0)
        {
            LblError.Visible = true;
            LblError.Text = "Please supply number of bedrooms.";
            return false;
        }
        int noBeds = 0;
        if (!Int32.TryParse(TxtNumberOfBedrooms.Text.Trim(), out noBeds))
        {
            LblError.Visible = true;
            LblError.Text = "Please supply a valid number of bedrooms.";
            return false;
        }
        if (Int32.TryParse(TxtNumberOfBedrooms.Text.Trim(), out noBeds))
        {
            if (noBeds == 0)
            {
                LblError.Visible = true;
                LblError.Text = "Please supply a valid number of bedrooms.";
                return false;
            }
        }
        return true;
    }
    private PomplingProperty SavePropertyLetInfo()
    {
        PomplingProperty p = new PomplingProperty();
        string sAddressId = WebhelperMethods.GetUrlParamValue(Globals.URL_VAR_ADDRESS_ID, Request);       

        int addressID = Int32.Parse(sAddressId);
        p.PomplingAddressID = addressID;
        p.NumberOfBedrooms = (TxtNumberOfBedrooms.Text.Length > 0) ? Int32.Parse(TxtNumberOfBedrooms.Text) : 0;
        p.PricePerCalendarMonth = (TxtPricePerCalendarMonth.Text.Length > 0) ? decimal.Parse(TxtPricePerCalendarMonth.Text) : 0m;
        p.PropertyType = CmbPropertyType.DataTextField;
        string test = CmbPropertyType.SelectedItem.Text;
        p.PropertyType = test;
        p.Furnished = ChkIsFurnished.Checked;
        p.ShortDescription = TxtShortDescription.Text.Trim();
        p.LongDescription = TxtLongDescription.Text.Trim();
        p.CustomerID = Request["c"];
        p.SavePropertyLetinfo();
        return p;
    }
    protected void Previous_Click(object sender, EventArgs e)
    {
        string newUri = "admin_save_address.aspx" + Globals.BuildAdminURLParams(Request, WebhelperMethods.GetUrlParamValue(Globals.URL_VAR_ADDRESS_ID, Request)); ;
        Response.Redirect(newUri);    
    }
}